package base

import (
	"encoding/json"
	"gopkg.in/yaml.v3"
	"log"
	"os"
)

type MainConfig struct {
	ServerPort    int    `yaml:"ServerPort" json:"-"`
	Https         bool   `yaml:"Https" json:"-"`
	Debug         bool   `yaml:"Debug"`
	DBHost        string `yaml:"DBHost" json:"-"`
	DBUser        string `yaml:"DBUser" json:"-"`
	DBPassword    string `yaml:"DBPassword" json:"-"`
	DBName        string `yaml:"DBName" json:"-"`
	DynamicConfig `yaml:",inline"`
}

type DynamicConfig struct {
	Facades                []string `yaml:"Facades"`                // 前端的访问入口
	CoinExchangeRate       [2]int   `yaml:"CoinExchangeRate"`       // 铜板-人民币的兑换率(仅供参考)
	DefaultHearsayMaxCoin  int      `yaml:"DefaultHearsayMaxCoin"`  // 解锁传闻所需的铜板数量最大值的默认值
	HearsayMinCoin         int      `yaml:"HearsayMinCoin"`         // 发布传闻最小的铜板
	ActivityCoin           int      `yaml:"ActivityCoin"`           // 打卡奖励
	ReferreeRegisterCoin   int      `yaml:"ReferreeRegisterCoin"`   // 拉新注册的奖励
	ReferreeActionCoin     int      `yaml:"ReferreeActionCoin"`     // 拉新时各种任务的默认奖励
	RecommendHearsayFilter bool     `yaml:"RecommendHearsayFilter"` // 推荐模式下过滤掉刷到过的帖子
	Tops                   []Top    `yaml:"Tops"`                   // 置顶帖
}

type Top struct {
	Icon      string `yaml:"Icon"`
	Title     string `yaml:"Title"`
	GuideName string `yaml:"GuideName"`
	HearsayId int    `yaml:"HearsayId"`
	Url       string `yaml:"Url"`
}

var Config = func() MainConfig {
	c := MainConfig{
		DynamicConfig: DynamicConfig{
			CoinExchangeRate:       [2]int{1, 1},
			DefaultHearsayMaxCoin:  20,
			HearsayMinCoin:         0,
			ActivityCoin:           1,
			ReferreeRegisterCoin:   1,
			ReferreeActionCoin:     1,
			RecommendHearsayFilter: true,
			Tops:                   []Top{},
		},
	}

	data, err := os.ReadFile(WorkDir + "config.yaml")
	if err != nil {
		log.Fatalln("无法读取到配置文件, err:" + err.Error())
	}
	if err := yaml.Unmarshal(data, &c); err != nil {
		log.Fatalln("解析配置文件失败, err:" + err.Error())
	}
	if c.ServerPort == 0 {
		log.Fatalln("服务端口不能为0")
	}

	data, err = os.ReadFile(WorkDir + "config.json")
	if err == nil {
		_ = json.Unmarshal(data, &c.DynamicConfig)
	}
	return c
}()
